<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input v-model="queryParams.keywords" placeholder="关键字" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="hover" class="data-table">
      <el-table ref="dataTableRef" v-loading="loading" :data="tableList" highlight-current-row border
        class="data-table__content">
        <el-table-column label="合同名称" prop="contractName" min-width="200" />
        <el-table-column label="负责人" prop="majorUsername" />
        <el-table-column label="当前节点" prop="currentNode" />
        <el-table-column label="当前处理人" prop="currentUsers" />
        <el-table-column label="项目状态" prop="status" width="150">
          <template #default="scope">
            <el-tag v-if="scope.row.status" type="success">
              {{ ContractBuild_Status_Label[scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="派单时间" prop="dispatchTime" />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button @click="toDetail(scope.row)" type="primary" size="small" link icon="search">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="fetchData" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import BuildAPI, { BuildPageVO, ContractBuild_Status_Label, BuildPageQuery } from "@/api/flow/build.api";
import router from "@/router";
import { encryptDES } from "@/utils/des";

defineOptions({
  name: "BuildManagement",
  inheritAttrs: false,
});

const loading = ref(false);
const total = ref(0);
const tableList = ref<BuildPageVO[]>();
const queryParams = reactive<BuildPageQuery>({
  pageNum: 1,
  pageSize: 10,
  currentTask: true,
});

const queryFormRef = ref();

onMounted(() => {
  handleQuery();
});

// 查询（重置页码后获取数据）
function handleQuery() {
  queryParams.pageNum = 1;
  fetchData();
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  fetchData();
}

// 获取数据
function fetchData() {
  loading.value = true;
  BuildAPI.getPage(queryParams)
    .then((data) => {
      tableList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 跳转详情页面 */
function toDetail(row: BuildPageVO) {
  // 参数加密
  const param = { id: row.contractId };
  const jsonString = JSON.stringify(param);
  const paramStr = encryptDES(jsonString);
  router.push({ path: "/build/management/detail", query: { key: paramStr } });
}
</script>
